package com.zhr.tea.view.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.zhr.tea.R;
import com.zhr.tea.entity.User;
import com.zhr.tea.utils.IsLogin;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class RegestActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String SEND_URL = "http://www.teapaopao.com:8099/tea/androidMember/getAuthCode.do";
    public static final String SEND_YZM_URL = "http://www.teapaopao.com:8099/tea/androidMember/regNoPassword.do";
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.regest_btn_yanzheng)
    Button regestBtnYanzheng;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.regest_btn_start)
    Button regestBtnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regest);
        initView();
    }

    private void initView() {
        ButterKnife.bind(this);
        regestBtnYanzheng.setOnClickListener(this);
        regestBtnStart.setOnClickListener(this);
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 11) {
                    regestBtnYanzheng.setEnabled(true);
                } else {
                    regestBtnYanzheng.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regest_btn_yanzheng:
                yanzheng();
                break;
            case R.id.regest_btn_start:
                start();
                break;
        }

    }

    private void yanzheng() {
        Toast.makeText(this, "消息已发出，请注意查收", Toast.LENGTH_LONG).show();
        etPhone.setEnabled(false);
        regestBtnYanzheng.setEnabled(false);
        etYzm.setEnabled(true);
        send();

    }

    private void start() {
        if (etYzm.isEnabled()) {
            if (etYzm.getText().toString().length() != 0) {
                sendYZM();
            } else {
                Toast.makeText(this, "验证码不能为空", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
        }
    }

    private void sendYZM() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("versionCode", "54");
        obj.put("authCode", etYzm.getText().toString());
        obj.put("memberAccount", etPhone.getText().toString());
        obj.put("expandChannel", "yingyongbao");
        obj.put("uniqueFlag", "359125050312158");
        obj.put("phoneSystem", "2");

        params.applicationJson(obj);
        HttpRequest.post(SEND_YZM_URL, params, new BaseHttpRequestCallback<User>() {
            @Override
            protected void onSuccess(User user) {
                super.onSuccess(user);
                if (user.getResult().equals("000")) {
                    IsLogin.Login(user);
                    //储存用户信息

                    RegestActivity.this.finish();
                } else {
                    Toast.makeText(RegestActivity.this, "验证码不正确", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
                Toast.makeText(RegestActivity.this, "验证码不正确", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void send() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        String s = etPhone.getText().toString();
        obj.put("memberPhone", s);
        params.applicationJson(obj);
        HttpRequest.post(SEND_URL, params, new BaseHttpRequestCallback());

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }


}
